﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Mocola.Common;
using System.Data;
using System.Data.Common;
using Mocola.BLL;
using Mocola.Model;
using Mocola.Web;

namespace Mocola.Net.Web
{
    public partial class application : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string UserID = MocolaUser.CurrentUser.UserID.ToString();

            string DepartList = string.Empty;

            string sql = "SELECT [Name] FROM Moc_Departments WHERE [ID] in (SELECT DepartID FROM Moc_DepartmentUser WHERE UserID = {0})";
            sql = string.Format(sql, UserID);
            IDataReader reader = Databases.MocolaNet.ExecuteReader(CommandType.Text, sql);
            while (reader.Read())
            {
                DepartList += reader[0].ToString() + ";";
            }

            txtBuMen.Text = DepartList;                             // 申请部门
            txtShenQingRen.Text = MocolaUser.CurrentUser.FullName;  // 申请人
            txtRiQi.Text = DateTime.Now.ToShortDateString();        // 申请日期
            txtGuiHuaiRiQi.Text = DateTime.Now.AddDays((Request.QueryString["tp"] == "M" ? 30 : 15)).ToShortDateString();// 归还日期
        }

        protected void btnShenQing_Click(object sender, EventArgs e)
        {
            string BuMen = txtBuMen.Text.Trim();
            string ShenQingRen = txtShenQingRen.Text.Trim();
            DateTime ShenQingRiQi = Convert.ToDateTime(txtRiQi.Text.Trim());
            string YuanYin = txtYuanYin.Text.Trim();
            int FangShi = Convert.ToInt32(rblShenQingFangShi.SelectedValue);
            bool NR_YuanJian = cblShenQingNeiRong.Items[0].Selected ? true : false;   // 原件
            bool NR_FuYinJian = cblShenQingNeiRong.Items[1].Selected ? true : false;  // 复印件
            bool NR_GuangPan = cblShenQingNeiRong.Items[2].Selected ? true : false;   // 光盘
            int ShuLiang = Convert.ToInt32(txtShuLiang.Text.Trim()); /* 数量验证： */if (ShuLiang == 0) { Alert.Show("请填写正确的数量！"); return; }
            DateTime GuiHuanRiQi = Convert.ToDateTime(txtGuiHuaiRiQi.Text.Trim());
            int ShenQingRenID = MocolaUser.CurrentUser.UserID;
            string FenLei = Request.QueryString["tp"].ToString();       // M:图纸 F:文件
            int FenLeiID = Convert.ToInt32(Request.QueryString["id"]);  // 图纸ID/文件ID
            string LieBie = Request.QueryString["lb"].ToString();       // P:项目类 M:管理类

            MembershipCreateStatus status = new MembershipCreateStatus();
            DbTransaction transaction = Databases.MocolaNet.BeginTransaction(IsolationLevel.ReadCommitted);

            try
            {
                int nID = Databases.MocolaNet.Insert(Moc_UseBorrowInfo.Moc_UseBorrow)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.BuMen, BuMen)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.ShenQingRen, ShenQingRen)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.ShenQingRiQi, ShenQingRiQi)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.YuanYin, YuanYin)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.FangShi, FangShi)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.NR_YuanJian, NR_YuanJian)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.NR_FuYinJian, NR_FuYinJian)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.NR_GuangPan, NR_GuangPan)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.ShuLiang, ShuLiang)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.GuiHuanRiQi, GuiHuanRiQi)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.ShenQingRenID, ShenQingRenID)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.FenLei, FenLei)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.FenLeiID, FenLeiID)
                        .AddColumn(Moc_UseBorrowInfo.Moc_UseBorrow.LieBie, LieBie)
                        .SetTransaction(transaction)
                        .ExecuteReturnAutoIncrementID(Moc_UseBorrowInfo.Moc_UseBorrow.ID);

                bool blTrees = false;

                if (nID != -1)
                {
                    blTrees = true;
                }


                if (blTrees)
                {
                    Alert.Show("成功提交申请，请等待管理员审核！您可以在借阅管理->借阅记录里查看您的借阅日志。");
                    status = MembershipCreateStatus.Success;
                }
                else
                {
                    Alert.Show("申请提交失败！请联系管理员解决。");
                    status = MembershipCreateStatus.ProviderError;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                status = MembershipCreateStatus.ProviderError;
            }
            finally
            {
                if (status == MembershipCreateStatus.Success)
                    transaction.Commit();
                else
                    transaction.Rollback();
                Databases.MocolaNet.CloseConnection(transaction);
            }

        }
    }
}
